package com.ruoyi.wechat.api;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Goods;

import com.ruoyi.system.domain.GoodsImge;
import com.ruoyi.system.service.IGoodsService;
import com.ruoyi.wechat.domain.WxFacilityType;
import com.ruoyi.wechat.service.impl.QRCodeGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.ruoyi.common.core.domain.AjaxResult.success;

@Api(value = "微信小程序API-商品管理", tags = {"微信小程序API-商品管理"})
@RestController
@RequestMapping("/wechat/goods")
public class WxGoodsController {
    @Autowired
    IGoodsService goodsService;
@Autowired
    QRCodeGenerator qrCodeGenerator;
    @ApiOperation(value = "获取商品列表", response = WxFacilityType.class)
    @GetMapping("/list")
    public AjaxResult list(Goods goods) {
        System.out.println("123123123123");
        System.out.println("goods:"+goods);
        List<Goods> list  = goodsService.selectGoodsList(goods);
        System.out.println("listgoods:"+list);
        return success(list);
    }

    /**
     * 新增商品表
     */

    @PostMapping
    public AjaxResult add(@RequestBody Goods goods)
    {
        System.out.println("wwwwky:"+goods);
//goods.setStatus(0);
        return success(goodsService.insertGoods(goods));
    }

//删除
    @DeleteMapping("/delete/{goodsIds}")
    public AjaxResult remove(@PathVariable String[] goodsIds)
    {
        System.out.println("1231231234444444444");
        return success(goodsService.deleteGoodsByGoodsIds(goodsIds));
    }

    @GetMapping("/download-qr-code")
    public ResponseEntity<byte[]> downloadQRCode(@RequestParam String text) {
        try {
            byte[] qrCodeImage = qrCodeGenerator.generateQRCodeImage(text);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.IMAGE_PNG);
            headers.setContentDispositionFormData("attachment", "qrcode.png");
            return new ResponseEntity<>(qrCodeImage, headers, HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}
